<?php

// No direct access
defined('_JEXEC') or die;

jimport('joomla.application.component.view');

class ApkipasViewApkipas extends JView
{		
	function display($tpl = null) {
	
		$this->setModel($this->getModel(), true);
		$fnc = 'display'.ucfirst($this->getLayout());
		if (is_callable(array(&$this, $fnc)))
			$this->$fnc($tpl);
		else
			parent::display($tpl);
	}
	
	function displayDefault($tpl = null){
	
		// Initialise variables
		$items		= $this->get('Data');
		
		// Check for errors.
		if (count($errors = $this->get('Errors'))) {
			JError::raiseWarning(500, implode("\n", $errors));
			return false;
		}

		if ($items === false) {
			return JError::raiseError(404, JText::_('JGLOBAL_CATEGORY_NOT_FOUND'));
		}

		$app = JFactory::getApplication('site');
		$params =  & $app->getParams('com_apkipas');

		jimport('joomla.html.pagination');
		$limitstart = JRequest::getVar('limitstart');
		$pagination = new JPagination(10000, $limitstart, 10);

		$this->_prepareDocument($params);
		$this->assignRef('params', $params);
		$this->assignRef('items', $items);
		$this->assignRef('pagination', $pagination);
		parent::display($tpl);
	}

	protected function _prepareDocument($params)
	{
		$document = &JFactory::getDocument();
		$app = JFactory::getApplication('site');
		$menus	= $app->getMenu();
		$title	= null;

		$menu = $menus->getActive();

		if($menu)
		{
			$params->def('page_heading', $params->def('page_title', $menu->title));
		} else {
			$params->def('page_heading', 'APKIPAS');
		}
		
		$title = $params->get('page_title', '');
		
		if (empty($title)) {
			$title = $app->getCfg('sitename');
		}
		elseif ($app->getCfg('sitename_pagetitles', 0) == 1) {
			$title = JText::sprintf('JPAGETITLE', $app->getCfg('sitename'), $title);
		}
		elseif ($app->getCfg('sitename_pagetitles', 0) == 2) {
			$title = JText::sprintf('JPAGETITLE', $title, $app->getCfg('sitename'));
		}
		
		$document->setTitle($title);

		if ($params->get('menu-meta_description'))
		{
			$document->setDescription($params->get('menu-meta_description'));
		}

		if ($params->get('menu-meta_keywords'))
		{
			$document->setMetadata('keywords', $params->get('menu-meta_keywords'));
		}

		if ($params->get('robots'))
		{
			$document->setMetadata('robots', $params->get('robots'));
		}
	}
}
